<template>
    <div>
        <el-card class="box-card">
            <span style="display: flex;justify-content: flex-start;align-items: center;"><span
                    style="margin-right: 10px;color: red;">{{ info.shareUserName }}</span>的
                <span style="margin: 0 10px;">
                    <dict-tag :options="dict.type.share_type" :value="info.type" />
                </span>
                <span style="margin: 0 10px;">
                    <dict-tag :options="dict.type.time_limit" :value="info.timeType" />
                </span>
                分享：</span>
            <div style="margin: 30px;">
                分享的文件：
                <el-button size="mini" type="text" @click="download()">{{ info.filemgr.name }}</el-button>
            </div>

            <el-button size="mini" type="text" @click="download()">点击下载</el-button>
        </el-card>
    </div>
</template>
<script>
import axios from 'axios'
import { Message } from 'element-ui'
import { saveAs } from 'file-saver'
import { getToken } from '@/utils/auth'
import errorCode from '@/utils/errorCode'
import { blobValidate } from "@/utils/ruoyi";
import {
    getSharemgr
} from "@/api/filemgr/sharemgr";
export default {
    name: "Index",
    dicts: ["share_type", "time_limit"],
    components: {
    },
    data() {
        return {
            shareId: undefined,
            info: {}
        };
    },
    created() {
        const shareId = this.$route.query.id;
        this.shareId = shareId;
        this.getInfo(shareId)
    },
    methods: {
        getInfo(shareId) {
            getSharemgr(shareId).then(res => {
                this.info = res.data
            })
        },
        download(id) {
            if (this.$store.state.user.id) {
                this.resource(this.shareId)
            } else {
                this.$confirm("请登录后再进行回复", "提示", {
                    confirmButtonText: "去登陆",
                    cancelButtonText: "取消",
                    type: "warning",
                })
                    .then(() => {
                        location.href = "/login?redirect=" + "/download?id=" + this.shareId;
                    })
                    .catch(() => { });
            }
        },
        async printErrMsg(data) {
            const resText = await data.text();
            const rspObj = JSON.parse(resText);
            const errMsg = errorCode[rspObj.code] || rspObj.msg || errorCode['default']
            Message.error(errMsg);
        },
        resource(id) {
            var url = process.env.VUE_APP_BASE_API + "/filemgr/sharemgr/download?shareId=" + id;
            axios({
                method: 'get',
                url: url,
                responseType: 'blob',
                headers: { 'Authorization': 'Bearer ' + getToken() }
            }).then((res) => {
                console.log(res)
                const isBlob = blobValidate(res.data);
                if (isBlob) {
                    const blob = new Blob([res.data])
                    saveAs(blob, decodeURIComponent(res.headers['download-filename']))
                } else {
                    this.printErrMsg(res.data);
                }
            })
        },
    },
};
</script>
<style scoped lang="scss">
.box-card {

    width: 100%;
}
</style>
  